package com.ycsj.goldmedalnewconcept.teacher.activity;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.ycsj.common.base.BaseActivity;
import com.ycsj.common.manager.BqaManager;
import com.ycsj.common.manager.SrfManager;
import com.ycsj.common.utils.GsonUtil;
import com.ycsj.common.utils.StateBarUtil;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.common.bean.TkItemBean;
import com.ycsj.goldmedalnewconcept.teacher.adapter.AssignQuestionRvAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 教师端-题库-练习预览
 * Created by Kevin on 2019/3/13
 */
public class TeacherTKHomeworkPreviewActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.rv)
    RecyclerView rv;

    @BindView(R.id.srf)
    SmartRefreshLayout srf;

    @BindView(R.id.rl_loading)
    RelativeLayout rlLoading;

    @BindView(R.id.rl_layout)
    RelativeLayout rl_layout;


    private TextView tvTitle;

    private ImageView ivBack;

    private ImageView lvPreview;

    //列表数据
    public List<TkItemBean> questionOldDatas;

    //列表适配器
    private AssignQuestionRvAdapter questionAdapter;

    //携带的题目
    private String questionList;

    //学生草稿信息
    private String stel;

    //题目草稿信息
    private String content;

    //总数
    private String total;

    //携带的单元号
    private String unit;

    @Override
    protected int getContentLayout() {
        return R.layout.activity_teacher_tk_homework_preview;
    }

    @Override
    protected void init() {

        //初始化视图
        initView();

        //通知栏沉浸式
        initStateBar();

        //初始化标题栏
        initTitle();

        //设置题目
        initQuestions();
    }


    /**
     * 设置题目
     */
    private void initQuestions() {

        rl_layout.setVisibility(View.VISIBLE);
        rlLoading.setVisibility(View.GONE);

        //获取题目
        questionList = getIntent().getStringExtra("questionList");
        stel = getIntent().getStringExtra("stel");
        total = getIntent().getStringExtra("total");
        content = getIntent().getStringExtra("content");
        unit = getIntent().getStringExtra("unit");

        questionOldDatas = new ArrayList<>();

        questionOldDatas = GsonUtil.jsonToList(questionList, TkItemBean[].class);

        //设置数据
        initData();
    }


    /**
     * 设置题目数据
     */
    private void initData() {

        //关闭下拉刷新
        SrfManager.setSmartByPullListener(srf, null);

        questionAdapter = new AssignQuestionRvAdapter(TeacherTKHomeworkPreviewActivity.this, R.layout.item_rv_assign_question, questionOldDatas, 1);

        BqaManager.setRv(null, TeacherTKHomeworkPreviewActivity.this, questionAdapter, rv);

    }


    /**
     * 初始化视图的方法
     */
    public void initView() {
        //标题栏
        tvTitle = findViewById(R.id.tv_title);
        ivBack = findViewById(R.id.iv_back);
        lvPreview = findViewById(R.id.lv_edit);
        ivBack.setOnClickListener(this);
        lvPreview.setOnClickListener(this);

    }


    /**
     * 初始化标题栏
     */
    private void initTitle() {
        tvTitle.setText("已选题目");
        lvPreview.setVisibility(View.VISIBLE);
        lvPreview.setImageResource(R.drawable.img_create);
    }

    /**
     * 沉浸状态栏
     */
    protected void initStateBar() {
        StateBarUtil.customImmerseBar(this);
    }


    /**
     * 页面控件点击事件
     *
     * @param v
     */
    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.iv_back:
                //退出
                finish();
                break;
            case R.id.lv_edit:
                //创建
                if (TextUtils.isEmpty(stel)) {
                    ToastUtils.showShort("请选择要布置练习的学生");
                } else {
                    //创建练习
                    Intent intent = new Intent(this, TeacherCreateHomeworkActivity.class);
                    intent.putExtra("stel", stel);
                    intent.putExtra("total", total + "");
                    intent.putExtra("content", content);
                    intent.putExtra("unit", unit + "");

                    startActivity(intent);

                    finish();
                }


                break;
        }
    }
}
